/**
 * 替换数据标签
 * GET参数:{get.name}
 * POST参数:{post.name}
 * Cookie参数:{cookie.name}
 * Session参数:{session.name}
 * Application参数:{application.name}
 *
 * @level 3
 */
package aorey.database;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import aorey.util.UUIDTool;

import java.util.HashMap;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DataTag {

	protected HttpServletRequest request;
	// 匹配标记的正则表达式
	// protected String reg =
	// "\\{(get|post|cookie|session|application)\\.(\\w+)\\s*\\}";
	protected String reg = "\\{(\\w+)\\.(\\w+)\\s*\\}";

	public DataTag(HttpServletRequest request) {
		this.request = request;
	}

	public String tagReplace(String body) {
		Pattern pattern = Pattern.compile(this.reg);
		Matcher matcher = pattern.matcher(body);
		String tagType;
		String tagName;
		// int tagCount = matcher.groupCount();
		String getValue = "";
		while (matcher.find()) {
			getValue = "";
			if (matcher.group(1).equals("get") || matcher.group(1).equals("post")) {
				getValue = this.request.getParameter(matcher.group(2));
				if (getValue == null) {
					getValue = "";
				}
				body = body.replace(matcher.group(0), getValue);
			} else if (matcher.group(1).equals("session")) {
				try {
					getValue = this.request.getSession().getAttribute(matcher.group(2)).toString();
				} catch (Exception e) {
					getValue = null;
				}
				if (getValue == null) {
					getValue = "";
				}

				body = body.replace(matcher.group(0), getValue);

			} else if (matcher.group(1).equals("cookie")) {
				Cookie[] cookies = this.request.getCookies();
				tagName = matcher.group(2);
				int cookie_len = cookies.length;
				for (int i = 0; i < cookie_len; i++) {
					Cookie cookie = cookies[i];
					if (cookie.getName().equals(tagName)) {
						getValue = cookie.getValue();
						break;
					}
				}
				body = body.replace(matcher.group(0), getValue);
			} else if (matcher.group(1).equals("app")) {
				getValue = this.request.getServletContext().getAttribute(matcher.group(2)).toString();
				body = body.replace(matcher.group(0), getValue);
			}
		}
		return body;
	}

	/**
	 * 根据数据替换标记
	 * 
	 * @param str
	 * @param data
	 * @return
	 */
	public String dataReplace(String str, JSONObject jsonData) {
//		System.out.println("传入str:" + str);
		String reg = "\\{\\$(\\w+)\\.(\\w+)\\.(\\w+)\\}";
		Pattern pattern = Pattern.compile(reg);
		Matcher matcher = pattern.matcher(str);
		String tag, tagReplace, tagName, nodeName, fieldName, tmp;
		tmp = this.pidReplace(str);
		int x = 1;
		while (matcher.find()) {
//			System.out.println("第"+x+"次匹配");
//			System.out.println("x->" + matcher.group());
//			System.out.println("0->" + matcher.group(0));
//			System.out.println("1->" + matcher.group(1));
//			System.out.println("2->" + matcher.group(2));
//			System.out.println("3->" + matcher.group(3));
//			System.out.println("--------------------------");
			
			tagName = matcher.group(0);
			tag = matcher.group(1);
//			nodeName = matcher.group(2);
			fieldName = matcher.group(3);
			switch (tag) {
			case "record":
				tmp = tmp.replace(tagName, jsonData.getString(fieldName));
				break;
			default:
				break;
			}
			x++;
		}
		return tmp;
	}
	
	/**
	 * 替换pid标记
	 * @param str
	 * @return
	 */
	public String pidReplace(String str)
	{
		return str.replace("{$pid}", UUIDTool.getUUID());
	}
}
